package cn.iocoder.yudao.module.alipay.controller.admin.infoslideshow;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.alipay.controller.admin.infoslideshow.vo.*;
import cn.iocoder.yudao.module.alipay.dal.dataobject.infoslideshow.InfoSlideshowDO;
import cn.iocoder.yudao.module.alipay.convert.infoslideshow.InfoSlideshowConvert;
import cn.iocoder.yudao.module.alipay.service.infoslideshow.InfoSlideshowService;

@Tag(name = "管理后台 - 轮播图")
@RestController
@RequestMapping("/alipay/info-slideshow")
@Validated
public class InfoSlideshowController {

    @Resource
    private InfoSlideshowService infoSlideshowService;

    @PostMapping("/create")
    @Operation(summary = "创建轮播图")
    @PreAuthorize("@ss.hasPermission('alipay:info-slideshow:create')")
    public CommonResult<Long> createInfoSlideshow(@Valid @RequestBody InfoSlideshowCreateReqVO createReqVO) {
        return success(infoSlideshowService.createInfoSlideshow(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新轮播图")
    @PreAuthorize("@ss.hasPermission('alipay:info-slideshow:update')")
    public CommonResult<Boolean> updateInfoSlideshow(@Valid @RequestBody InfoSlideshowUpdateReqVO updateReqVO) {
        infoSlideshowService.updateInfoSlideshow(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除轮播图")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('alipay:info-slideshow:delete')")
    public CommonResult<Boolean> deleteInfoSlideshow(@RequestParam("id") Long id) {
        infoSlideshowService.deleteInfoSlideshow(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得轮播图")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('alipay:info-slideshow:query')")
    public CommonResult<InfoSlideshowRespVO> getInfoSlideshow(@RequestParam("id") Long id) {
        InfoSlideshowDO infoSlideshow = infoSlideshowService.getInfoSlideshow(id);
        return success(InfoSlideshowConvert.INSTANCE.convert(infoSlideshow));
    }

    @GetMapping("/list")
    @Operation(summary = "获得轮播图列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('alipay:info-slideshow:query')")
    public CommonResult<List<InfoSlideshowRespVO>> getInfoSlideshowList(@RequestParam("ids") Collection<Long> ids) {
        List<InfoSlideshowDO> list = infoSlideshowService.getInfoSlideshowList(ids);
        return success(InfoSlideshowConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/getListByTypeId")
    @Operation(summary = "根据分类id获取轮播图组")
    @PreAuthorize("@ss.hasPermission('alipay:info-slideshow:query')")
    public CommonResult<List<InfoSlideshowRespVO>> getListByTypeId(@RequestParam("typeId")  String typeId) {
        List<InfoSlideshowDO> list = infoSlideshowService.getListByTypeId(typeId);
        return success(InfoSlideshowConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得轮播图分页")
    @PreAuthorize("@ss.hasPermission('alipay:info-slideshow:query')")
    public CommonResult<PageResult<InfoSlideshowRespVO>> getInfoSlideshowPage(@Valid InfoSlideshowPageReqVO pageVO) {
        PageResult<InfoSlideshowDO> pageResult = infoSlideshowService.getInfoSlideshowPage(pageVO);
        return success(InfoSlideshowConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出轮播图 Excel")
    @PreAuthorize("@ss.hasPermission('alipay:info-slideshow:export')")
    @OperateLog(type = EXPORT)
    public void exportInfoSlideshowExcel(@Valid InfoSlideshowExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<InfoSlideshowDO> list = infoSlideshowService.getInfoSlideshowList(exportReqVO);
        // 导出 Excel
        List<InfoSlideshowExcelVO> datas = InfoSlideshowConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "轮播图.xls", "数据", InfoSlideshowExcelVO.class, datas);
    }

}
